package com.etb.wisdom.eframe.org.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import com.etb.web.action.ETBAction;
import com.etb.web.eui.utils.EuiUtil;
import com.etb.wisdom.eframe.auth.AuthConstant.AuthType;
import com.etb.wisdom.eframe.auth.AuthConstant.PartyType;
import com.etb.wisdom.eframe.model.OrgOrganization;
import com.etb.wisdom.eframe.model.OrgPosition;
import com.etb.wisdom.eframe.org.service.OrgPositionService;

@Action("posi")
@Namespace("/eframe/org/position")
public class OrgPositionAction extends ETBAction<OrgPosition> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6211332595850509923L;
	
	@Autowired
	public OrgPositionService posiService;
	
	public String addView() {
		request.setAttribute("posilevel", request.getParameter("posilevel"));
		String uplevelid = request.getParameter("posiid");
		if(StringUtils.isNotBlank(uplevelid)) {
			OrgPosition posi = new OrgPosition();
			posi.setId(uplevelid);
			service.extend(posi);
			request.setAttribute("uplevelPosi", posi);
		}
		String orgid = request.getParameter("orgid");
		OrgOrganization org = new OrgOrganization();
		org.setId(orgid);
		service.extend(org);
		request.setAttribute("org", org);
		return ADD;
	}
	
	public String updateView() {
		super.updateView();
		String uplevelid = dataObject.getUplevelid();
		if(StringUtils.isNotBlank(uplevelid)) {
			OrgPosition posi = new OrgPosition();
			posi.setId(uplevelid);
			service.extend(posi);
			request.setAttribute("uplevelPosi", posi);
		}
		OrgOrganization org = new OrgOrganization();
		org.setId(dataObject.getOrgid());
		service.extend(org);
		request.setAttribute("org", org);
		return UPDATE;
	}
	
	@Override
	public void save() {
		posiService.save(dataObject);
	}
	
	public void getUnauthList() {
		String posiid = request.getParameter("posiid");
		Map<String, Object> hqlParams = new HashMap<String, Object>();
		hqlParams.put("partyid", posiid);
		hqlParams.put("partytype", PartyType.position.toString());
		hqlParams.put("authtype", AuthType.role.toString());
		List list = service.queryByNamedsql("query.eframe.framework.auth.getUnauthList", hqlParams);
		EuiUtil.toGrid(list, null);
	}
	
	public void getAuthList() {
		String posiid = request.getParameter("posiid");
		Map<String, Object> hqlParams = new HashMap<String, Object>();
		hqlParams.put("partyid", posiid);
		hqlParams.put("partytype", PartyType.position.toString());
		hqlParams.put("authtype", AuthType.role.toString());
		List list = service.queryByNamedsql("query.eframe.framework.auth.getAuthList", hqlParams);
		EuiUtil.toGrid(list, null);
	}
	
	public void saveRole() {
		String addRoleIds = request.getParameter("addRoleIds");
		String delRoleIds = request.getParameter("delRoleIds");
		String posiId = request.getParameter("posiid");
		posiService.saveRole(posiId, addRoleIds, delRoleIds);
	}
}